﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_BLL;
using QLTV_Public;

namespace GUI
{
    public partial class frmCapNhatTinhTrangTL : DevExpress.XtraEditors.XtraForm
    {
        #region Khai báo biến
        DataTable dt;
        TextMessageBox box = new TextMessageBox();
        DangKyCaBiet_BLL dkcb = new DangKyCaBiet_BLL();
        public string soCaBiet = "";
        private bool checkTT;
        #endregion

        #region LoadForm

        public frmCapNhatTinhTrangTL()
        {
            InitializeComponent();
        }

        private void frmCapNhatTinhTrangTL_Load(object sender, EventArgs e)
        {
            LoadData();
        }
        #endregion

        #region các chức năng

        private void btnLuuThoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            UpdateData();
        }

        private void btnDong_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }
        #endregion

        #region các sự kiện

        private void ckbTinhTrang_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbTinhTrang.CheckState == CheckState.Checked)
                checkTT = true;
            else
                checkTT = false;

        }
        #endregion 

        #region ham viet them
        private void UpdateData()
        {
            DangKyCaBiet_Public dk = new DangKyCaBiet_Public();
            string[]s_CaBiet=soCaBiet.Split(',');
            if (soCaBiet.Length <= 16)//update 1 ma ca biet
            {
                
                dk.TinhTrangTaiLieu = txtTinhTrang.Text;
                dk.TrangThaiSuDung = checkTT;
                dk.SoCaBiet = s_CaBiet[0];

                dkcb.Update_TinhTrangTL(dk, dk, dk);

                XtraMessageBox.Show("Cập nhật thành công!",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
               
            }
            else//update nhieu
            {
                for (int i = 0; i < s_CaBiet.Length - 1; i++)
                {
                    dk.TinhTrangTaiLieu = txtTinhTrang.Text;
                    dk.TrangThaiSuDung = checkTT;
                    dk.SoCaBiet = s_CaBiet[i];

                    dkcb.Update_TinhTrangTL(dk, dk, dk);

                }
                XtraMessageBox.Show("Cập nhật thành công!", box.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            DialogResult = DialogResult.OK;

        }

        private void setInterface()
        {
            txtSoCaBiet.Text = "";
            txtTinhTrang.Text = "";
            ckbTinhTrang.CheckState = CheckState.Unchecked;
        }
        private void LoadData()
        {
            if (soCaBiet.Length<=16)
            {
                //tach
                string[] s_CaBiet = soCaBiet.Split(',');
                //
                dt = dkcb.TaoBang("select b.SoCaBiet,a.NhanDe,b.TrangThaiSuDung,b.TinhTrangTaiLieu from  TaiLieu a join DangKyCaBiet b on a.MaTaiLieu=b.MaTaiLieu where b.SoCaBiet='"+s_CaBiet[0]+"'");
                DataRow dr = dt.Rows[0];
                txtSoCaBiet.Text=dr["SoCaBiet"].ToString();
                txtTinhTrang.Text = dr["TinhTrangTaiLieu"].ToString();
                string check=dr["TrangThaiSuDung"].ToString();
                if (check == "True")
                    ckbTinhTrang.CheckState = CheckState.Checked;
                else
                    ckbTinhTrang.CheckState = CheckState.Unchecked;
            }
            else
                if (soCaBiet.Length > 15)
                {
                    txtSoCaBiet.Text = soCaBiet;
                }
 
        }

        #endregion




    }
}